'use strict'

let util = require('util');

function formatTime(date) {
    return formatHourMinute(date.getHours(), date.getMinutes());
}

function formatDate(date) {
    return util.format('%d年%d月%d日', date.getFullYear(), date.getMonth() + 1, date.getDate());
}

function formatHourMinute(hour, minute) {
    return util.format('%s:%s', paddingZero(hour, 2), paddingZero(minute, 2));
}

function paddingZero(number, width) {
    let numberString = String(number);
    if ( numberString.length < width ) {
        numberString = '0'.repeat(width - numberString.length) + numberString;
    }

    return numberString;
}

function randomString(len) {
　　len = len || 32;
　　let $chars = 'ABCDEFabcdef0123456789';
　　let maxPos = $chars.length;
　　let pwd = '';
　　for ( let i = 0; i < len; i++) {
　　　　pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
　　}
　　return pwd;
}

function currentTimestamp() {
    let now = new Date();

    return now.getTime();
}

function concatArrays(arrays) {
    return arrays.reduce((last, current) => {
        return last.concat(current);
    }, []);
}

module.exports = {
    randomString,
    currentTimestamp,
    formatTime,
    formatHourMinute,
    formatDate,
    concatArrays
}
